package com.unitech.demo01.controller;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

@RestController
@RequestMapping("/user")
public class HelloController {

    @RequestMapping("/login")
    public String login() {
        System.out.println("user login..");
        return "login success";
    }

    @GetMapping("/register1")
    public void register1(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //获取请求参数
        String username = request.getParameter("username");

        if (username.equals("zhangsan")) {
            response.getWriter().println("<font color='red'>用户名已被注册！</font>");
        } else {
            response.getWriter().println("<font color='green'>用户名可用！</font>");
        }
    }

    @PostMapping("/register2")
    public void register2(@RequestParam("username")String username,HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        //获取请求参数
        if (username.equals("zhangsan")) {
            response.getWriter().println("<font color='red'>用户名已被注册！</font>");
        } else {
            response.getWriter().println("<font color='green'>用户名可用！</font>");
        }
    }
}
